﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcContacts.Models
{
    public class EF_ContactRepository: IContactRepository
    {
        private ContactEntities _db = new ContactEntities();

        public void CreateNewContact(Contact contactToCreate)
        {
            _db.Contacts.AddObject(contactToCreate);
            _db.SaveChanges();
        }

        public void DeleteContact(int id)
        {
            var conToDel = GetContactById(id);
            _db.Contacts.DeleteObject(conToDel);
            _db.SaveChanges();
        }

        public Contact GetContactById(int id)
        {
            return _db.Contacts.FirstOrDefault(d => d.Id == id);
        }

        public IEnumerable<Contact> GetAllContacts()
        {
            return _db.Contacts.ToList();
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }
    }
}